package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.entity.LoginRegisterEntity;

import java.sql.Connection;
import java.util.List;
import java.util.Objects;

public class LoginRegisterDao {
    public List<LoginRegisterEntity> list() {
        //操作数据库
        //获取连接
        Connection conn = JdbcUtilsV1.getConnection();
        String sql = "select * from admins ";
        return JdbcUtilsV1.executeQuery(conn, LoginRegisterEntity.class, sql);
    }
// 登录
    public LoginRegisterEntity login(String username, String password, String role) {
        Connection conn = JdbcUtilsV1.getConnection();
        String sql ="select * from ";
//        if(role.equals("管理员")){
//            sql = "select id, username, password, where username = ? and password = ?";
//        } else if (role.equals("商家")) {
//            sql = "select id, username, password, contact_phone, real_name as realName, avatar_url as avatarUrl, merchant_name as merchantName, contact_person as contactPerson, contact_phone as contactPhone, address, business_license as businessLicense, logo_url as logoUrl from merchant where username = ? and password = ? and status = 1";
        if(role.equals("管理员")){
        sql += "admin where username = ? and password = ?";
    } else if (role.equals("商家")) {
        sql += " merchant where username = ? and password = ? and status = 1";
    }else if (role.equals("用户")) {
            sql = "select id, username, password, phone, real_name as realName, id_card_encrypted as idCard, avatar_url as avatarUrl, driver_license_encrypted as driverLicenseEncrypted from user where username = ? and password = ? and status = 1";
        } else {
            return null;
        }
        return JdbcUtilsV1.executeQueryOne(conn, LoginRegisterEntity.class, sql, username, password);
    }
//    注册
    public boolean insert(LoginRegisterEntity user) {
        String sql = null;
        if (Objects.equals(user.getRole(), "用户")) {
            sql = "insert into user (username,password,phone,real_name,id_card_encrypted,avatar_url,driver_license_encrypted) values(?,?,?,?,?,?,?)";
             JdbcUtilsV1.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getPhone(), user.getRealName(), user.getIdCard(), user.getAvatarUrl(), user.getDriverLicenseEncrypted());

        }else if(Objects.equals(user.getRole(), "商家")) {
            sql = "insert into merchant_application (username,password,merchant_name,contact_person,contact_phone,address,business_license,logo_url) values(?,?,?,?,?,?,?,?)";
            JdbcUtilsV1.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getMerchantName(), user.getContactPerson(), user.getContactPhone(), user.getAddress(), user.getBusinessLicense(), user.getLogoUrl());
        }
        return true;
    }
}
